//
// Created by Jisam on 21/09/2024 8:08 PM.
// Solution of  货仓选址
#include <bits/stdc++.h>
using namespace std;


int main() {
    int N;
    cin >> N;
    vector<int> A(N);

    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }

    sort(A.begin(), A.end());

    long long ans = 0;
    int mid = 0;
    if (N % 2 == 1) {
        mid = A[N / 2];
        ans = accumulate(A.begin(), A.end(), 0LL, [&](long long acc, int val) {
            return acc + abs(val - mid);
        });
    } else {
        int mid1 = A[(N / 2) - 1];
        int mid2 = A[N / 2];
        long long sum1 = 0, sum2 = 0;
        for (int i = 0; i < N; ++i) {
            sum1 += abs(A[i] - mid1);
            sum2 += abs(A[i] - mid2);
        }
        ans = min(sum1, sum2);
    }

    cout << ans << endl;

    return 0;
}